package com.yuexunit.finance.member.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.member.entity.FinanceBulkstockPledgeRateEntity;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
*@version 创建时间：2016年10月11日 下午5:25:32
*@author NTBK-021-1006
*类说明
*/

@Implements(FinanceBulkstockPledgeRateService.class)
public class FinanceBulkstockPledgeRateServiceImpl implements FinanceBulkstockPledgeRateService {

	@Override
	@Transactional
	public Long createFinanceBulkstockPledgeRate(FinanceBulkstockPledgeRateEntity entity) {
		PersistUtil.insertOrUpdate(entity);
		return entity.getPledgeRateId();
	}

	@Override
	@Transactional
	public void updateFinanceBulkstockPledgeRate(FinanceBulkstockPledgeRateEntity entity) {
		PersistUtil.update(entity);
	}

	@Override
	public FinanceBulkstockPledgeRateEntity inquireFinanceBulkstockPledgeRate(Long pledgeRateId) {
		return PersistUtil.get(FinanceBulkstockPledgeRateEntity.class, pledgeRateId);
	}

	@Override
	public List<FinanceBulkstockPledgeRateEntity> inquireFinanceBulkstockPledgeRateList(Long gradeValuationId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockPledgeRateEntity.class);
		criteria.add(Restrictions.eq("gradeEvaluationId", gradeValuationId));
		return PersistUtil.get(criteria);
	}

	@Override
	@Transactional
	public void deleteFinanceBulkstockPledgeRate(Long pledgeRateId) {
		PersistUtil.delete(FinanceBulkstockPledgeRateEntity.class, pledgeRateId);
	}

	@Override
	public FinanceBulkstockPledgeRateEntity inquireFinanceBulkstockPledgeRate(Long gradeValuationId,
			Long bulkstockInfoId, ProductTypeEnum productType) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockPledgeRateEntity.class);
		criteria.add(Restrictions.eq("gradeEvaluationId", gradeValuationId));
		criteria.add(Restrictions.eq("bulkstockInfoId", bulkstockInfoId));
		criteria.add(Restrictions.eq("productType", productType));
		criteria.add(Restrictions.le("effectiveStartDate", DateUtil.parseDateInt(DateUtil.nowDate())));
		criteria.add(Restrictions.ge("effectiveEndDate", DateUtil.parseDateInt(DateUtil.nowDate())));
		List<FinanceBulkstockPledgeRateEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public FinanceBulkstockPledgeRateEntity existBulkstockPledgeRate(Long bulkstockInfoId, Long gradeEvaluationId, Integer productType) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockPledgeRateEntity.class);
		criteria.add(Restrictions.eq("bulkstockInfoId", bulkstockInfoId));
		criteria.add(Restrictions.eq("gradeEvaluationId", gradeEvaluationId));
		criteria.add(Restrictions.le("effectiveStartDate", DateUtil.parseDateInt(DateUtil.nowDate())));
		criteria.add(Restrictions.ge("effectiveEndDate", DateUtil.parseDateInt(DateUtil.nowDate())));
		criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		List<FinanceBulkstockPledgeRateEntity> list = PersistUtil.get(criteria);
		if (list == null || list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}
}
